<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" label-width="130px" :model="queryParams" :inline="true">
            <el-form-item label="视频标题" prop="title">
              <el-input v-model="queryParams.title" placeholder="请输入视频标题" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="分辨率" prop="resolution">
              <el-input v-model="queryParams.resolution" placeholder="请输入分辨率" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="视频格式" prop="videoFormat">
              <el-input v-model="queryParams.videoFormat" placeholder="请输入视频格式" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="文件大小" prop="size">
              <el-input v-model="queryParams.size" placeholder="请输入文件大小" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="所属合集" prop="collectionId">
              <el-select v-model="queryParams.collectionId" placeholder="请选择所属合集" clearable @keyup.enter="handleQuery">
                <el-option v-for="item in collectionList" :key="item.id" :label="item.title" :value="item.id" />
              </el-select>
            </el-form-item>
            <el-form-item label="视频分区" prop="categoryId">
              <el-select v-model="queryParams.categoryId" placeholder="请选择视频分区" clearable @keyup.enter="handleQuery">
                <el-option v-for="item in categoryList" :key="item.id" :label="item.categoryName" :value="item.id" />
              </el-select>
            </el-form-item>
            <el-form-item label="视频时长" prop="duration">
              <el-input v-model="queryParams.duration" placeholder="请输入视频时长" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="视频类型" prop="videoType">
              <el-select v-model="queryParams.videoType" placeholder="请选择视频类型" clearable @keyup.enter="handleQuery">
                <el-option v-for="dict in video_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="二创设置" prop="isRemix">
              <el-select v-model="queryParams.isRemix" placeholder="请选择二创设置" clearable @keyup.enter="handleQuery">
                <el-option v-for="dict in yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="可见范围" prop="visibility">
              <el-select v-model="queryParams.visibility" placeholder="请选择可见范围" clearable @keyup.enter="handleQuery">
                <el-option v-for="dict in visibility" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="是否关闭弹幕" prop="disableDanmaku">
              <el-select v-model="queryParams.disableDanmaku" placeholder="请选择是否关闭弹幕" clearable @keyup.enter="handleQuery">
                <el-option v-for="dict in yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="是否关闭评论" prop="disableComment">
              <el-select v-model="queryParams.disableComment" placeholder="请选择是否关闭评论" clearable @keyup.enter="handleQuery">
                <el-option v-for="dict in yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="是否开启精选评论" prop="enableFeaturedComments">
              <el-select v-model="queryParams.enableFeaturedComments" placeholder="请选择是否开启精选评论" clearable @keyup.enter="handleQuery">
                <el-option v-for="dict in yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="视频状态" prop="videoStatus">
              <el-select v-model="queryParams.videoStatus" placeholder="请选择视频状态" clearable @keyup.enter="handleQuery">
                <el-option v-for="dict in video_status" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="播放次数" prop="playCount">
              <el-input v-model="queryParams.playCount" placeholder="请输入播放次数" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="弹幕数量" prop="danmakuCount">
              <el-input v-model="queryParams.danmakuCount" placeholder="请输入弹幕数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="评论数量" prop="commentCount">
              <el-input v-model="queryParams.commentCount" placeholder="请输入评论数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="点赞数量" prop="likeCount">
              <el-input v-model="queryParams.likeCount" placeholder="请输入点赞数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="投币数量" prop="coinCount">
              <el-input v-model="queryParams.coinCount" placeholder="请输入投币数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="收藏数量" prop="favoriteCount">
              <el-input v-model="queryParams.favoriteCount" placeholder="请输入收藏数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="分享次数" prop="shareCount">
              <el-input v-model="queryParams.shareCount" placeholder="请输入分享次数" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <!-- <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['business:video:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['business:video:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['business:video:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['business:video:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template> -->

      <el-table v-loading="loading" height="800" :data="videoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column fixed label="UP主" align="center" prop="createByName" width="180" show-overflow-tooltip />
        <el-table-column fixed label="视频标题" align="center" prop="title" />
        <el-table-column fixed label="封面图片" width="100" align="center" prop="coverUrl">
          <template #default="scope">
            <el-image
              lazy
              preview-teleported
              :preview-src-list="[scope.row.coverUrl]"
              style="width: 60px; z-index: 999 !important"
              :src="scope.row.coverUrl"
            />
          </template>
        </el-table-column>
        <el-table-column fixed label="视频文件" width="200" align="center" prop="fileUrl">
          <template #default="scope">
            <div style="width: 100%; aspect-ratio: 16 / 9; position: relative; overflow: hidden">
              <video
                :src="scope.row.fileUrl"
                :poster="scope.row.coverUrl"
                preload="none"
                controls
                style="position: absolute; top: 0; left: 0; width: 100%; height: 100%"
              ></video>
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed label="视频简介" align="center" prop="description" width="180" show-overflow-tooltip />
        <el-table-column label="分辨率" align="center" prop="resolution" />
        <el-table-column label="视频格式" align="center" prop="videoFormat" />
        <el-table-column label="文件大小" align="center" prop="size" />
        <el-table-column label="所属合集" align="center" prop="collectionName" />
        <el-table-column label="视频分区" align="center" prop="categoryName" />
        <el-table-column label="视频时长" align="center" prop="duration" />
        <el-table-column label="视频类型" align="center" prop="videoType">
          <template #default="scope">
            <dict-tag :options="video_type" :value="scope.row.videoType" />
          </template>
        </el-table-column>
        <el-table-column label="二创设置" align="center" prop="isRemix">
          <template #default="scope">
            <dict-tag :options="yes_no" :value="scope.row.isRemix" />
          </template>
        </el-table-column>
        <el-table-column label="可见范围" align="center" prop="visibility">
          <template #default="scope">
            <dict-tag :options="visibility" :value="scope.row.visibility" />
          </template>
        </el-table-column>

        <el-table-column label="是否关闭弹幕" align="center" prop="disableDanmaku" width="120">
          <template #default="scope">
            <dict-tag :options="yes_no" :value="scope.row.disableDanmaku" />
          </template>
        </el-table-column>
        <el-table-column label="是否关闭评论" align="center" prop="disableComment" width="120">
          <template #default="scope">
            <dict-tag :options="yes_no" :value="scope.row.disableComment" />
          </template>
        </el-table-column>
        <el-table-column label="是否开启精选评论" align="center" prop="enableFeaturedComments" width="130">
          <template #default="scope">
            <dict-tag :options="yes_no" :value="scope.row.enableFeaturedComments" />
          </template>
        </el-table-column>
        <el-table-column label="视频状态" align="center" prop="videoStatus">
          <template #default="scope">
            <dict-tag :options="video_status" :value="scope.row.videoStatus" />
          </template>
        </el-table-column>
        <el-table-column label="播放次数" align="center" prop="playCount" />
        <el-table-column label="弹幕数量" align="center" prop="danmakuCount" />
        <el-table-column label="评论数量" align="center" prop="commentCount" />
        <el-table-column label="点赞数量" align="center" prop="likeCount" />
        <el-table-column label="投币数量" align="center" prop="coinCount" />
        <el-table-column label="收藏数量" align="center" prop="favoriteCount" />
        <el-table-column label="分享次数" align="center" prop="shareCount" />
        <el-table-column label="声明与权益信息" align="center" prop="copyrightInfo" width="180" show-overflow-tooltip />
        <el-table-column label="转载来源信息" align="center" prop="sourceInfo" width="180" show-overflow-tooltip />
        <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
        <el-table-column fixed="right" label="操作" align="center" width="110" class-name="small-padding fixed-width">
          <template #default="scope">
            <!-- <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:video:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:video:remove']"></el-button>
            </el-tooltip> -->
            <el-tooltip content="弹幕" placement="top">
              <el-button
                link
                type="primary"
                icon="ChatLineRound"
                @click="handleDanmaku(scope.row)"
                v-hasPermi="['business:videoDanmaku:list']"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="评论" placement="top">
              <el-button
                link
                type="primary"
                icon="ChatDotSquare"
                @click="handleComment(scope.row)"
                v-hasPermi="['business:videoDanmaku:list']"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="互动" placement="top">
              <el-button
                link
                type="primary"
                icon="Pointer"
                @click="handleInteraction(scope.row)"
                v-hasPermi="['business:contentInteraction:list']"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改视频基础信息对话框 -->
    <!-- <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="videoFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="视频标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入视频标题" />
        </el-form-item>
        <el-form-item label="视频简介" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="分辨率" prop="resolution">
          <el-input v-model="form.resolution" placeholder="请输入分辨率" />
        </el-form-item>
        <el-form-item label="视频格式" prop="videoFormat">
          <el-input v-model="form.videoFormat" placeholder="请输入视频格式" />
        </el-form-item>
        <el-form-item label="文件大小" prop="size">
          <el-input v-model="form.size" placeholder="请输入文件大小" />
        </el-form-item>
        <el-form-item label="所属合集id" prop="collectionId">
          <el-input v-model="form.collectionId" placeholder="请输入所属合集id" />
        </el-form-item>
        <el-form-item label="视频分区 ID" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入视频分区 ID" />
        </el-form-item>
        <el-form-item label="视频时长" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入视频时长" />
        </el-form-item>
        <el-form-item label="视频类型：0-自制，1-转载" prop="videoType">
          <el-select v-model="form.videoType" placeholder="请选择视频类型：0-自制，1-转载">
            <el-option v-for="dict in video_type" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="转载来源信息" prop="sourceInfo">
          <el-input v-model="form.sourceInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="二创设置：0-否，1-是" prop="isRemix">
          <el-input v-model="form.isRemix" placeholder="请输入二创设置：0-否，1-是" />
        </el-form-item>
        <el-form-item label="可见范围：0-公开，1-私密，2-仅粉丝可见" prop="visibility">
          <el-input v-model="form.visibility" placeholder="请输入可见范围：0-公开，1-私密，2-仅粉丝可见" />
        </el-form-item>
        <el-form-item label="声明与权益信息" prop="copyrightInfo">
          <el-input v-model="form.copyrightInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否关闭弹幕：0-否，1-是" prop="disableDanmaku">
          <el-input v-model="form.disableDanmaku" placeholder="请输入是否关闭弹幕：0-否，1-是" />
        </el-form-item>
        <el-form-item label="是否关闭评论：0-否，1-是" prop="disableComment">
          <el-input v-model="form.disableComment" placeholder="请输入是否关闭评论：0-否，1-是" />
        </el-form-item>
        <el-form-item label="是否开启精选评论：0-否，1-是" prop="enableFeaturedComments">
          <el-input v-model="form.enableFeaturedComments" placeholder="请输入是否开启精选评论：0-否，1-是" />
        </el-form-item>
        <el-form-item label="视频状态：0-草稿,1-待审核,2-已发布,3-下架" prop="videoStatus">
          <el-radio-group v-model="form.videoStatus">
            <el-radio v-for="dict in video_status" :key="dict.value" :value="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="播放次数" prop="playCount">
          <el-input v-model="form.playCount" placeholder="请输入播放次数" />
        </el-form-item>
        <el-form-item label="弹幕数量" prop="danmakuCount">
          <el-input v-model="form.danmakuCount" placeholder="请输入弹幕数量" />
        </el-form-item>
        <el-form-item label="评论数量" prop="commentCount">
          <el-input v-model="form.commentCount" placeholder="请输入评论数量" />
        </el-form-item>
        <el-form-item label="点赞数量" prop="likeCount">
          <el-input v-model="form.likeCount" placeholder="请输入点赞数量" />
        </el-form-item>
        <el-form-item label="投币数量" prop="coinCount">
          <el-input v-model="form.coinCount" placeholder="请输入投币数量" />
        </el-form-item>
        <el-form-item label="收藏数量" prop="favoriteCount">
          <el-input v-model="form.favoriteCount" placeholder="请输入收藏数量" />
        </el-form-item>
        <el-form-item label="分享次数" prop="shareCount">
          <el-input v-model="form.shareCount" placeholder="请输入分享次数" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog> -->

    <!-- 弹幕列表弹框 -->
    <el-dialog title="视频弹幕" v-model="videoDanmukuDialog">
      <el-table v-loading="videoDanmakuLoading" :data="videoDanmakuList">
        <el-table-column label="弹幕内容" align="center" prop="content" show-overflow-tooltip>
          <template #default="scope">
            <span :style="{ color: scope.row.color }">{{ scope.row.content }}</span>
          </template>
        </el-table-column>
        <el-table-column label="弹幕颜色" align="center" prop="color">
          <template #default="scope">
            <div style="width: 20px; height: 20px; margin: 0 auto" :style="{ backgroundColor: scope.row.color }"></div>
          </template>
        </el-table-column>
        <el-table-column label="弹幕出现时间点(秒)" align="center" prop="videoTime" />
        <el-table-column label="发送者" align="center" prop="createByName" width="180" show-overflow-tooltip />
        <el-table-column label="发送时间" align="center" prop="createTime" width="180" show-overflow-tooltip />
      </el-table>
      <pagination
        v-show="videoDanmakuTotal > 0"
        :total="videoDanmakuTotal"
        v-model:page="videoDanmakuQueryParams.pageNum"
        v-model:limit="videoDanmakuQueryParams.pageSize"
        @pagination="getVideoDanmakuList"
      />
    </el-dialog>

    <!-- 评论列表弹框 -->
    <el-dialog title="评论列表" v-model="contentCommentDialog" width="1100px">
      <el-table
        v-loading="contentCommentLoading"
        :data="contentCommentList"
        row-key="id"
        lazy
        :load="loadChildrenCommentList"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column label="评论内容" align="center" prop="commentText" show-overflow-tooltip width="180" />
        <el-table-column label="内容类型" align="center" prop="contentType">
          <template #default="scope">
            <dict-tag :options="content_type" :value="scope.row.contentType" />
          </template>
        </el-table-column>
        <el-table-column label="点赞数" align="center" prop="likeCount" />
        <el-table-column label="回复数" align="center" prop="replyCount" />
        <el-table-column label="是否置顶" align="center" prop="isTop">
          <template #default="scope">
            <dict-tag :options="yes_no" :value="scope.row.isTop" />
          </template>
        </el-table-column>
        <el-table-column label="是否精选" align="center" prop="isFeatured">
          <template #default="scope">
            <dict-tag :options="yes_no" :value="scope.row.isFeatured" />
          </template>
        </el-table-column>
        <el-table-column label="评论者" align="center" prop="createByName" show-overflow-tooltip width="180" />
        <el-table-column label="评论时间" align="center" prop="createTime" show-overflow-tooltip width="180" />
      </el-table>
      <pagination
        v-show="contentCommentTotal > 0"
        :total="contentCommentTotal"
        v-model:page="contentCommentQueryParams.pageNum"
        v-model:limit="contentCommentQueryParams.pageSize"
        @pagination="getContentCommentList"
      />
    </el-dialog>

    <!-- 视频互动行为弹窗 -->
    <el-dialog title="互动行为记录" v-model="contentInteractionDialog" width="1100px">
      <el-table v-loading="contentInteractionLoading" :data="contentInteractionList">
        <el-table-column label="互动类型" align="center" prop="actionType">
          <template #default="scope">
            <dict-tag :options="action_type" :value="scope.row.actionType" />
          </template>
        </el-table-column>
        <el-table-column label="互动值" align="center" prop="actionValue" />
        <el-table-column label="互动时间" align="center" prop="actionTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.actionTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="投币类型" align="center" prop="coinType">
          <template #default="scope">
            <dict-tag :options="coin_type" :value="scope.row.coinType" />
          </template>
        </el-table-column>
        <el-table-column label="转发附加内容" align="center" prop="forwardContent" show-overflow-tooltip />
      </el-table>
      <pagination
        v-show="contentInteractionTotal > 0"
        :total="contentInteractionTotal"
        v-model:page="contentInteractionQueryParams.pageNum"
        v-model:limit="contentInteractionQueryParams.pageSize"
        @pagination="getContentInteractionList"
      />
    </el-dialog>
  </div>
</template>

<script setup name="Video" lang="ts">
import { listVideo, getVideo, delVideo, addVideo, updateVideo } from '@/api/business/video';
import { VideoVO, VideoQuery, VideoForm } from '@/api/business/video/types';
import { listContentCategory } from '@/api/business/contentCategory';
import { listContentCollection } from '@/api/business/contentCollection';
import { ContentCollectionVO } from '@/api/business/contentCollection/types';
import { ContentCategoryVO } from '@/api/business/contentCategory/types';
import { listVideoDanmaku } from '@/api/business/videoDanmaku';
import { listContentComment } from '@/api/business/contentComment';
import { VideoDanmakuQuery, VideoDanmakuVO } from '@/api/business/videoDanmaku/types';
import { ContentCommentQuery, ContentCommentVO } from '@/api/business/contentComment/types';
import { listContentInteraction } from '@/api/business/contentInteraction';
import { ContentInteractionVO } from '@/api/business/contentInteraction/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { video_type, video_status, yes_no, visibility, content_type, action_type, coin_type } = toRefs<any>(
  proxy?.useDict('video_type', 'video_status', 'yes_no', 'visibility', 'content_type', 'action_type', 'coin_type') || {}
);

const videoList = ref<VideoVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const videoFormRef = ref<ElFormInstance>();

const collectionList = ref<ContentCollectionVO[]>([]);
const categoryList = ref<ContentCategoryVO[]>([]);

const videoDanmukuDialog = ref(false);
const videoDanmakuLoading = ref(false);
const contentCommentDialog = ref(false);
const contentCommentLoading = ref(false);
const videoDanmakuList = ref<VideoDanmakuVO[]>([]);
const contentCommentList = ref<ContentCommentVO[]>([]);
const videoDanmakuTotal = ref<number>(0);
const videoDanmakuQueryParams = ref<VideoDanmakuQuery>({
  pageNum: 1,
  pageSize: 10
});

const contentCommentTotal = ref<number>(0);
const contentCommentQueryParams = ref<ContentCommentQuery>({
  pageNum: 1,
  pageSize: 10,
  contentType: 1, // 1-视频
  parentId: 0
});

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

// 获取视频评论回复列表
const childrenCommentQueryParams = ref<ContentCommentQuery>({
  pageNum: 1,
  pageSize: 10,
  contentType: 1 // 1-视频
});

// 视频互动行为
const contentInteractionLoading = ref(false);
const contentInteractionDialog = ref(false);
const contentInteractionList = ref<ContentInteractionVO[]>([]);
const contentInteractionTotal = ref<number>(0);
const contentInteractionQueryParams = ref<ContentCommentQuery>({
  pageNum: 1,
  pageSize: 10,
  contentType: 1 // 1-视频
});

const initFormData: VideoForm = {
  id: undefined,
  title: undefined,
  description: undefined,
  resolution: undefined,
  videoFormat: undefined,
  size: undefined,
  collectionId: undefined,
  categoryId: undefined,
  duration: undefined,
  videoType: undefined,
  sourceInfo: undefined,
  isRemix: undefined,
  visibility: undefined,
  copyrightInfo: undefined,
  disableDanmaku: undefined,
  disableComment: undefined,
  enableFeaturedComments: undefined,
  videoStatus: undefined,
  playCount: undefined,
  danmakuCount: undefined,
  commentCount: undefined,
  likeCount: undefined,
  coinCount: undefined,
  favoriteCount: undefined,
  shareCount: undefined,
  remark: undefined
};
const data = reactive<PageData<VideoForm, VideoQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: undefined,
    description: undefined,
    resolution: undefined,
    videoFormat: undefined,
    size: undefined,
    collectionId: undefined,
    categoryId: undefined,
    duration: undefined,
    videoType: undefined,
    sourceInfo: undefined,
    isRemix: undefined,
    visibility: undefined,
    copyrightInfo: undefined,
    disableDanmaku: undefined,
    disableComment: undefined,
    enableFeaturedComments: undefined,
    videoStatus: undefined,
    playCount: undefined,
    danmakuCount: undefined,
    commentCount: undefined,
    likeCount: undefined,
    coinCount: undefined,
    favoriteCount: undefined,
    shareCount: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '视频 ID不能为空', trigger: 'blur' }],
    title: [{ required: true, message: '视频标题不能为空', trigger: 'blur' }],
    fileUrl: [{ required: true, message: '视频文件 URL不能为空', trigger: 'blur' }],
    resolution: [{ required: true, message: '分辨率不能为空', trigger: 'blur' }],
    videoFormat: [{ required: true, message: '视频格式不能为空', trigger: 'blur' }],
    size: [{ required: true, message: '文件大小不能为空', trigger: 'blur' }],
    categoryId: [{ required: true, message: '视频分区 ID不能为空', trigger: 'blur' }],
    duration: [{ required: true, message: '视频时长不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询视频基础信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listVideo(queryParams.value);
  videoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  videoFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: VideoVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加视频基础信息';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: VideoVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getVideo(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改视频基础信息';
};

/** 提交按钮 */
const submitForm = () => {
  videoFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateVideo(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addVideo(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: VideoVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除视频基础信息编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delVideo(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'business/video/export',
    {
      ...queryParams.value
    },
    `video_${new Date().getTime()}.xlsx`
  );
};

// 弹幕列表
const handleDanmaku = (row: VideoVO) => {
  videoDanmakuQueryParams.value.videoId = row.id;
  getVideoDanmakuList();
  videoDanmukuDialog.value = true;
};

// 评论列表
const handleComment = (row: VideoVO) => {
  contentCommentQueryParams.value.contentId = row.id;
  childrenCommentQueryParams.value.contentId = row.id;
  getContentCommentList();
  contentCommentDialog.value = true;
};

// 互动列表
const handleInteraction = (row: VideoVO) => {
  contentInteractionDialog.value = true;
  contentInteractionQueryParams.value.contentId = row.id;
  getContentInteractionList();
};

// 加载子评论列表
const loadChildrenCommentList = (row: ContentCommentVO, treeNode: unknown, resolve: (data: ContentCommentVO[]) => void) => {
  childrenCommentQueryParams.value.parentId = row.id;
  listContentComment(childrenCommentQueryParams.value).then((res) => {
    resolve(res.rows);
  });
};

// 获取视频弹幕列表
const getVideoDanmakuList = async () => {
  videoDanmakuLoading.value = true;
  const res = await listVideoDanmaku(videoDanmakuQueryParams.value);
  videoDanmakuTotal.value = res.total;
  videoDanmakuList.value = res.rows;
  videoDanmakuLoading.value = false;
};

// 获取视频评论列表
const getContentCommentList = async () => {
  contentCommentLoading.value = true;
  const res = await listContentComment(contentCommentQueryParams.value);
  contentCommentTotal.value = res.total;
  contentCommentList.value = res.rows;
  contentCommentLoading.value = false;
};

// 获取视频互动列表
const getContentInteractionList = async () => {
  contentInteractionLoading.value = true;
  const res = await listContentInteraction(contentInteractionQueryParams.value);
  contentInteractionList.value = res.rows;
  contentInteractionTotal.value = res.total;
  contentInteractionLoading.value = false;
};

onMounted(() => {
  getList();
  listContentCollection().then((res) => {
    collectionList.value.push(...res.rows);
  });
  listContentCategory().then((res) => {
    categoryList.value.push(...res.rows);
  });
});
</script>
